package cn.crdeng.controller;

import cn.crdeng.Service.BlogService;
import cn.crdeng.Service.TagService;
import cn.crdeng.po.Blog;
import cn.crdeng.po.BlogQuery;
import cn.crdeng.po.Tag;
import cn.crdeng.po.Type;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;

@Controller
public class TagShowController {

    @Autowired
    private TagService tagService;
    @Autowired
    private BlogService blogService;
    @GetMapping("/tags/{id}")
    public String getTags(@PageableDefault(size = 5, sort = {"updateTime"}, direction = Sort.Direction.DESC) Pageable pageable,
                          @PathVariable Long id, Model model) {
        List<Tag> tags = tagService.listTag(1000);
        //如果为id为-1则是首页跳转至此页
        if (id==-1){
            id=tags.get(0).getId();
        }
        Page<Blog> pageInfo = blogService.listBlogByTagId(pageable, id);
        model.addAttribute("tags",tags);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("activeId",id);
        return "tags";
    }

}
